package com.yyplatform.admin.platform.role.controller;

import static com.yyplatform.admin.Constants.URL_PREFIX;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yyplatform.admin.infra.model.RestResponse;
import com.yyplatform.admin.platform.role.model.dto.SysRoleDTO;
import com.yyplatform.admin.platform.role.model.dto.SysRoleFormDTO;
import com.yyplatform.admin.platform.role.model.dto.SysRoleMenuDTO;
import com.yyplatform.admin.platform.role.model.dto.SysRoleMenuFormDTO;
import com.yyplatform.admin.platform.role.model.dto.SysRoleQueryDTO;
import com.yyplatform.admin.platform.role.service.SysRoleService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2024-09-14
 */
@Slf4j
@Tag(name = "用户/角色管理")
@AllArgsConstructor
@RestController
@RequestMapping(URL_PREFIX + "/sys/role/")
public class SysRoleController {

    private SysRoleService sysRoleService;

    @GetMapping("query")
    @Operation(summary = "查询")
    @RequiresPermissions("sys:role:query")
    public RestResponse<Page<SysRoleDTO>> query(SysRoleQueryDTO pageQueryDTO) {
        log.debug("query dto = {}", pageQueryDTO);

        Page<SysRoleDTO> page = sysRoleService.query(pageQueryDTO);

        return RestResponse.ok(page);
    }

    @GetMapping("get")
    @Operation(summary = "通过ID获取详情")
    @RequiresPermissions("sys:role:get")
    public RestResponse<SysRoleDTO> get(Long id) {
        log.debug("get by id is {}", id);

        SysRoleDTO data = sysRoleService.get(id);
        return RestResponse.ok(data);
    }

    @GetMapping("options")
    @Operation(summary = "获取下拉options")
    @RequiresPermissions("sys:role:options")
    public RestResponse<List<SysRoleDTO>> options() {

        return RestResponse.ok(sysRoleService.getOptions());
    }


    @PostMapping("save")
    @Operation(summary = "保存")
    @RequiresPermissions("sys:role:save")
    public RestResponse<Void> save(@RequestBody SysRoleFormDTO formDTO) {
        log.debug("save dto is {}", formDTO);

        sysRoleService.save(formDTO);

        return RestResponse.ok(null);
    }


    @PostMapping("update")
    @Operation(summary = "修改")
    @RequiresPermissions("sys:role:update")
    public RestResponse<Void> update(@RequestBody SysRoleFormDTO formDTO) {
        log.debug("update dto is {}", formDTO);
        sysRoleService.update(formDTO);
        return RestResponse.ok(null);
    }


    @PostMapping("delete")
    @Operation(summary = "删除")
    @RequiresPermissions("sys:role:delete")
    public RestResponse<Void> delete(Long id) {
        log.debug("delete by id is {}", id);

        sysRoleService.delete(id);
        return RestResponse.ok(null);
    }


    @GetMapping("menu/query")
    @Operation(summary = "查询菜单权限")
    @RequiresPermissions("sys:role:menuQuery")
    public RestResponse<SysRoleMenuDTO> queryMenu(Long id) {
        log.debug("query menu by id is {}", id);
        SysRoleMenuDTO sysRoleMenuDTO = sysRoleService.queryMenu(id);

        return RestResponse.ok(sysRoleMenuDTO);
    }


    @PostMapping("menu/save")
    @Operation(summary = "保存菜单权限")
    @RequiresPermissions("sys:role:menuSave")
    public RestResponse<Void> saveMenu(@RequestBody SysRoleMenuFormDTO formDTO) {
        log.debug("save menu dto is {}", formDTO);

        sysRoleService.saveMenu(formDTO);

        return RestResponse.ok();
    }

}
